<template>
  <div class="contract-list-container">
    <el-card class="contract-list-header">
      <div class="title">
        <h2>项目合同列表</h2>
        <!-- <el-button type="primary" size="small" @click="handleAddContract">
          <el-icon><Plus /></el-icon>
          新增合同
        </el-button> -->
      </div>
      <div class="filters">
        <el-input
          v-model="searchKeyword"
          placeholder="搜索合同名称或编号"
          prefix-icon="Search"
          clearable
          @clear="handleSearch"
          @input="handleSearch"
          style="width: 250px;"
        />
        <el-select v-model="statusFilter" placeholder="合同状态" clearable @change="handleSearch">
          <el-option label="全部" value="" />
          <el-option label="执行中" value="executing" />
          <el-option label="已完成" value="completed" />
          <el-option label="已终止" value="terminated" />
        </el-select>
      </div>
    </el-card>

    <div class="contract-items">
      <el-row :gutter="20">
        <el-col :xs="24" :sm="12" :md="8" :lg="6" v-for="contract in filteredContracts" :key="contract.contractNo">
          <el-card class="contract-item" shadow="hover" @click="handleContractClick(contract)">
            <div class="contract-item-header">
              <span class="contract-name">{{ contract.contractName }}</span>
              <el-tag size="small" :type="getContractStatusType(contract.status)">{{ contract.status }}</el-tag>
            </div>
            <div class="contract-info">
              <div class="info-row">
                <span class="label">合同编号:</span>
                <span class="value">{{ contract.contractNo }}</span>
              </div>
              <div class="info-row">
                <span class="label">签订单位:</span>
                <span class="value">{{ contract.trustUnit }}</span>
              </div>
              <div class="info-row">
                <span class="label">签订时间:</span>
                <span class="value">{{ contract.signDate }}</span>
              </div>
              <div class="info-row">
                <span class="label">合同金额:</span>
                <span class="value amount">¥{{ formatNumber(contract.contractAmount) }}</span>
              </div>
            </div>
            <div class="contract-progress">
              <div class="progress-label">
                <span>支付进度</span>
                <span>{{ contract.paymentRatio }}%</span>
              </div>
              <el-progress 
                :percentage="contract.paymentRatio" 
                :color="getProgressColor(contract.paymentRatio)"
              />
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <div class="no-data" v-if="filteredContracts.length === 0">
      <el-empty description="暂无合同数据" />
    </div>
  </div>
</template>

<script>
export default {
  name: 'ContractList',
  data() {
    return {
      searchKeyword: '',
      statusFilter: '',
      contracts: [
        {
          contractNo: 'HT202312501',
          contractName: '智慧城市数据中台建设项目',
          contractType: '固定总价合同',
          trustUnit: '北京数字科技有限公司',
          signDate: '2023-12-05',
          contractAmount: 12580000,
          taxRate: '6%',
          depositAmount: 629000,
          receiptAmount: 5032000,
          nextPaymentAmount: 2516000,
          totalPaymentAmount: 5032000,
          paymentRatio: 40,
          status: '执行中'
        },
        {
          contractNo: 'HT202312502',
          contractName: '企业信息化系统升级项目',
          contractType: '固定总价合同',
          trustUnit: '上海网络科技有限公司',
          signDate: '2023-12-08',
          contractAmount: 8960000,
          taxRate: '6%',
          depositAmount: 448000,
          receiptAmount: 2688000,
          nextPaymentAmount: 1792000,
          totalPaymentAmount: 2688000,
          paymentRatio: 30,
          status: '执行中'
        },
        {
          contractNo: 'HT202312503',
          contractName: '物联网平台开发项目',
          contractType: '固定总价合同',
          trustUnit: '深圳智能技术有限公司',
          signDate: '2023-12-10',
          contractAmount: 6380000,
          taxRate: '6%',
          depositAmount: 319000,
          receiptAmount: 3190000,
          nextPaymentAmount: 1276000,
          totalPaymentAmount: 3190000,
          paymentRatio: 50,
          status: '执行中'
        },
        {
          contractNo: 'HT202312504',
          contractName: '云计算资源采购项目',
          contractType: '固定总价合同',
          trustUnit: '广州云服务科技有限公司',
          signDate: '2023-12-15',
          contractAmount: 4860000,
          taxRate: '6%',
          depositAmount: 243000,
          receiptAmount: 972000,
          nextPaymentAmount: 972000,
          totalPaymentAmount: 972000,
          paymentRatio: 20,
          status: '执行中'
        },
        {
          contractNo: 'HT202312505',
          contractName: '安防系统升级项目',
          contractType: '固定总价合同',
          trustUnit: '杭州安防科技有限公司',
          signDate: '2023-12-18',
          contractAmount: 3680000,
          taxRate: '6%',
          depositAmount: 184000,
          receiptAmount: 2944000,
          nextPaymentAmount: 736000,
          totalPaymentAmount: 2944000,
          paymentRatio: 80,
          status: '已完成'
        }
      ]
    }
  },
  computed: {
    filteredContracts() {
      let result = [...this.contracts];
      
      // 按状态筛选
      if (this.statusFilter) {
        result = result.filter(item => item.status === this.statusFilter);
      }
      
      // 按关键词搜索
      if (this.searchKeyword) {
        const keyword = this.searchKeyword.toLowerCase();
        result = result.filter(item => 
          item.contractName.toLowerCase().includes(keyword) || 
          item.contractNo.toLowerCase().includes(keyword) ||
          item.trustUnit.toLowerCase().includes(keyword)
        );
      }
      
      return result;
    }
  },
  methods: {
    formatNumber(num) {
      return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
    },
    getContractStatusType(status) {
      switch (status) {
        case '执行中':
          return 'primary';
        case '已完成':
          return 'success';
        case '已终止':
          return 'danger';
        default:
          return 'info';
      }
    },
    getProgressColor(percentage) {
      if (percentage < 30) {
        return '#909399';
      } else if (percentage < 70) {
        return '#409EFF';
      } else {
        return '#67C23A';
      }
    },
    handleContractClick(contract) {
      // 跳转到合同详情页面
      this.$router.push({
        path: '/contract',
        query: { id: contract.contractNo }
      });
    },
    handleSearch() {
      // 搜索处理，已通过计算属性实现
    },
    handleAddContract() {
      this.$message.info('新增合同功能正在开发中');
    }
  }
}
</script>

<style scoped lang="scss">
.contract-list-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 120px);
  
  .contract-list-header {
    margin-bottom: 20px;
    
    .title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 16px;
      
      h2 {
        margin: 0;
        font-size: 18px;
        color: #303133;
      }
    }
    
    .filters {
      display: flex;
      gap: 16px;
    }
  }
  
  .contract-items {
    margin-bottom: 20px;
    
    .contract-item {
      height: 100%;
      margin-bottom: 20px;
      cursor: pointer;
      transition: all 0.3s;
      
      &:hover {
        transform: translateY(-5px);
        box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
      }
      
      .contract-item-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 14px;
        
        .contract-name {
          font-size: 16px;
          font-weight: 600;
          color: #303133;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          max-width: 70%;
        }
      }
      
      .contract-info {
        margin-bottom: 16px;
        
        .info-row {
          display: flex;
          margin-bottom: 8px;
          font-size: 14px;
          
          .label {
            color: #909399;
            width: 80px;
          }
          
          .value {
            color: #606266;
            flex: 1;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            
            &.amount {
              color: #303133;
              font-weight: 500;
            }
          }
        }
      }
      
      .contract-progress {
        .progress-label {
          display: flex;
          justify-content: space-between;
          margin-bottom: 8px;
          font-size: 13px;
          color: #909399;
        }
      }
    }
  }
  
  .no-data {
    padding: 40px 0;
  }
}
</style> 